﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="author" content="Aowork">
    <meta name="description" content="">
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, maximum-scale=1.0">
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no">

    <title></title>

    <link href="../css/mobile.css" rel="stylesheet" />

    <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.1.0.js"></script>
    <script src="../js/zepto.min.js"></script>
    <script src="../js/zepto.swipe.js"></script>
    <script src="../js/common.js"></script>
    <script src="../js/config.js"></script>
    <script src="../js/uiExtend.js"></script>
    <script src="../js/wx_config.js"></script>

    <script type="text/javascript">
        jc.fnInit.push(function () {
            jc.ui.header.trigger("updateTitle", "订单确认");
        });
    </script>

    <script type="text/javascript">
        window.hasFetchMaterial = false;

        jc.fnInit.push(function () {
            jc.ui.header.trigger("getUser", function (user) {

                if (!user) {
                    errorRouter("login");
                    return;
                }

                var shoppingCartIds = jc.hash.get("shoppingCartIds");

                $.getJSON(window.path.shoppingCart, {
                    accessToken: user.accessToken,
                    userId: user.uid,
                    shoppingCartIds: shoppingCartIds
                }, function (result) {
                    if (result.success) {
                        var data = result.data;

                        var dataArray = [];
                        for (var i = 0, l = data.length; i < l; i++) {
                            if (data[i].courseInfo.fetchMaterial == 1) {
                                hasFetchMaterial = true
                            }
                            data[i].courseInfo.shoppingCartId = data[i].shoppingCartId;
                            data[i].courseInfo.initClass = "checked";
                            dataArray.push(data[i].courseInfo);
                        }

                        if (hasFetchMaterial) {
                            var $fetchMaterial = $("#fetchMaterial");
                            $fetchMaterial.show();
                        }






                        jc.ui.courseHolder.filter("source=shoppingCart", "setup", dataArray);
                    } else {
                        errorRouter(result.desc);
                    }
                });


            });
        });


        window.courseHolderItemChange = function ($item) {
            var shoppingCartIds = jc.hash.get("shoppingCartIds");
            jc.ui.header.trigger("getUser", function (user) {

                $.getJSON(window.path.calDiscount, {
                    accessToken: user.accessToken,
                    userId: user.uid,
                    shoppingCartIds: shoppingCartIds
                }, function (result) {
                    if (result.success) {
                        result.data.shoppingCartIds = shoppingCartIds;
                        jc.ui.orderConfirmFooter.trigger("setup", result.data);

                        var orderConfirmListData = {};
                        orderConfirmListData.list = $item;
                        orderConfirmListData.discount = result.data

                        jc.ui.orderConfirmList.trigger("setup", orderConfirmListData);
                    }
                });

            });


        }

        window.orderConfirmNext = function (shoppingCartIds) {
            jc.ui.header.trigger("getUser", function (user) {



                var data = {
                    accessToken: user.accessToken,
                    userId: user.uid,
                    shoppingCartIds: shoppingCartIds
                }

                function post(){
                    jc.loading.show();
                    $.ajax({
                        url: window.path.createOrder,
                        type: "POST",
                        data: data,
                        timeout: 60 * 1000,
                        success: function (result) {
                            jc.loading.hide();
                            if (result.success) {

                                var data = result.data;

                                var payUrl = data.payUrl;

                                if (payUrl == "") {
                                    /* 0元购买 去个人中心 */
                                    window.location.href = window.ctx + "home/courseList.html";
                                } else {

                                    /* 弹出支付是否完成 */
                                    //jc.ui.buyConfim.trigger("show");

                                    setTimeout(function () {
                                        if (window.isWeixn()) {
                                            payUrl = payUrl.replace("page_type=1",
                                                "page_type=3");
                                        } else {
                                            payUrl = payUrl.replace("page_type=1",
                                                "page_type=2");
                                        }
                                        window.location.href = payUrl;
                                    }, 1);
                                }

                                //window.location.href = payUrl;
                            } else {

                                if (result.desc == "购物车无内容,无法创建订单") {
                                    //alert("该课程已下单，请勿重新购买");
                                    alert("请至少选择一门课程");
                                } else if (result.desc.indexOf("课程已下单") != 1) {
                                    alert(result.desc);
                                    window.location.replace(window.ctx + "home/orderList.html");
                                } else {
                                    window.errorRouter(result.desc);
                                }

                            }
                        },
                        dataType: "json"


                    });
                }


                if (window.hasFetchMaterial) {
                    jc.ui.form.trigger("confim", function (confimData) {
                        if(confimData){
                            for(var attr in confimData){
                                data[attr] = confimData[attr];
                            }

                            post()
                        }
                        else{
                            alert("亲！请填写完成的收货信息")
                        }

                    });

                }else{
                    post()
                }








            });
        }
    </script>

</head>

<body>

    <div data-ui="header" class="J_header">
        <div class="h_inner"></div>
    </div>

    <div class="ptb10 bgfff bb1">

        <div data-ui="courseHolder" data-source="shoppingCart" data-keyword="title,price,label,signAmount" data-router="" class="J_courseHolder">
            <div class="c_inner">

            </div>
        </div>


    </div>


    <div class="bgfff bb1">
        <div data-ui="orderConfirmList" class="J_orderConfirmList">

        </div>
    </div>

    <div id="fetchMaterial" class="mt10 ptb10 bgfff bb1" style="display:none;">
        <div data-ui="form" class="J_form">
            <div data-id="receiverOfExpress" class="f_item">
                <div class="i_input">
                    <input placeholder="收件人" maxlength="16" type="text">
                </div>
            </div>
            <div data-id="phoneNoOfExpress" class="f_item">
                <div class="i_input">
                    <input placeholder="联系电话" maxlength="16" type="text">
                </div>
            </div>
            <div data-id="addressOfExpress" class="f_item">
                <div class="i_input">
                    <input placeholder="快递地址" maxlength="16" type="text">
                </div>
            </div>
        </div>
    </div>



    <div data-ui="orderConfirmFooter" class="J_orderConfirmFooter">
        <div class="o_inner">

        </div>
    </div>



    <div data-ui="buyConfim" class="J_buyConfim">
        <div class="b_inner">
            <div class="i_title">提示</div>
            <div class="i_txt">订单定否支付完成？</div>
            <div class="i_btn">
                <a class="abtn green" href="javascript:;">已支付完成</a>
                <a class="abtn gray" href="javascript:;">支付遇到问题?</a>
            </div>
        </div>
    </div>



    <div data-ui="footer" class="J_footer">


    </div>


    <div data-ui="nav" class="J_nav">
        <div class="n_inner">
            <div class="i_center clearfix"></div>
        </div>
    </div>




</body>

</html>